package com.guigu.tms.work.controller;

import com.guigu.tms.commons.entity.oms.Order;
import com.guigu.tms.commons.entity.work.TaskPickupDispatch;
import com.guigu.tms.commons.entity.work.TransportOrder;
import com.guigu.tms.work.mapper.OrderMapper;
import com.guigu.tms.work.service.ITaskPickupDispatchService;
import com.guigu.tms.work.service.ITransportOrderService;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 取件、派件任务信息表 前端控制器
 * </p>
 *
 * @author zwj
 * @since 2022-09-27
 */
@RestController
@RequestMapping("/taskPickupDispatch")
public class TaskPickupDispatchController {
    @Autowired
    ITaskPickupDispatchService pickupDispatchService;
    @Autowired
    ITransportOrderService transportOrderService;
    @Autowired
    StringRedisTemplate redisTemplate;
    private final String PICKUP_VALIDATECODE="PICKUP_VALIDATECODE";
    @RequestMapping("selectByCourierId")
public List<TaskPickupDispatch> selectByCourierId(String CourierId){
        System.out.println(CourierId);
    return pickupDispatchService.selectByCourierId(CourierId);
}
@RequestMapping("outPickUp")
public String outPickUp(@RequestBody TaskPickupDispatch taskPickupDispatch){
        taskPickupDispatch.setCourierId("");
        taskPickupDispatch.setAssignedStatus(3);
      pickupDispatchService.updateById(taskPickupDispatch);
        return "success";
}
    @RequestMapping("accptPickUp")
    public String accptPickUp(@RequestBody TaskPickupDispatch taskPickupDispatch){
        taskPickupDispatch.setStatus(2);
        taskPickupDispatch.setActualStartTime(new Date());
        pickupDispatchService.updateById(taskPickupDispatch);
        return "success";
    }
    @RequestMapping("Handover")
    public String Handover(@RequestBody TaskPickupDispatch taskPickupDispatch){
        taskPickupDispatch.setStatus(3);
        taskPickupDispatch.setActualStartTime(new Date());
        Order order = taskPickupDispatch.getOrder();
        order.setStatus(7);
        //交接和入库的方法体都一样 所以就用入库的方法了
        pickupDispatchService.PickupCompleted(taskPickupDispatch);
        return "success";
    }
    @RequestMapping("ConfirmDelivery")
    public String ConfirmDelivery(@RequestBody TaskPickupDispatch taskPickupDispatch){
        //妥投和入库的方法体都一样 所以就用入库的方法了 加上前端已经把对象设置好了 直接调用就行了
        pickupDispatchService.PickupCompleted(taskPickupDispatch);
        return "success";
    }
    @RequestMapping("Warehousing")
    public String Warehousing(@RequestBody TaskPickupDispatch taskPickupDispatch){
        //扫码入库：设置取件状态为4（已完成） 设置实际完成时间为现在
          taskPickupDispatch.setStatus(4);
          taskPickupDispatch.setActualEndTime(new Date());
        Order order = taskPickupDispatch.getOrder();
        // 设置订单（order）状态为4（网点入库）
        order.setStatus(4);
        //因为取件和入库都是对taskpickupdispathc表和order表进行update 所以用了同一个方法
        pickupDispatchService.PickupCompleted(taskPickupDispatch);
        //入库完成后要生成运单 所以还要调用TransportOrderService
        //创建对象
        TransportOrder transportOrder=new TransportOrder();
        transportOrder.setOrderId(taskPickupDispatch.getOrderId());
        transportOrder.setStatus(1);
        transportOrder.setSchedulingStatus(1);
        transportOrder.setCreateTime(new Date());
        //插入数据
        transportOrderService.save(transportOrder);
        return "success";
    }
    @RequestMapping("PickupCompleted")
    public String PickupCompleted(@RequestBody TaskPickupDispatch taskPickupDispatch){
//        修改订单状态  取件单设置status为3（待妥投） 设置取件时间为现在
        taskPickupDispatch.setStatus(3);
        taskPickupDispatch.setConfirmTime(new Date());
        Order order = taskPickupDispatch.getOrder();
        //判断付款方式 如果是预付的话 设置状态为已付款
       if(order.getPaymentMethod()==1){
           order.setPaymentStatus(2);
       }
        // 修改order的status为3（已取件）
        order.setStatus(3);
        taskPickupDispatch.setOrder(order);
        pickupDispatchService.PickupCompleted(taskPickupDispatch);
        return "success";
    }
    @RequestMapping("VerifyPickCode")
    public Boolean VerifyPickCode(@RequestBody Map<String,String>params){
        String orderId = params.get("orderId");
        String ValidateCode = params.get("ValidateCode");
        String s = redisTemplate.opsForValue().get(PICKUP_VALIDATECODE + orderId);
        if(s.equals("")||s==null){
            return false;
        }else{
            if(s.equals(ValidateCode)){
                redisTemplate.delete(PICKUP_VALIDATECODE+orderId);
                return true;
            }
            else{
                return false;
            }

        }
    }
}
